package com.zhongzhi.serviceimpl;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.dao.StationDao;
import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.dao.EquipmentDao;
import com.zhongzhi.dao.MeasureDao;
import com.zhongzhi.service.StationService;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.StationShow;
import com.zhongzhi.springboot.pojo.Type;
import com.zhongzhi.util.Bo;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import lombok.extern.log4j.Log4j;

@Log4j
@Service
public class StationServiceImpl implements StationService{

	@Autowired
	private StationDao stationDao;
	@Autowired
	private MeasureDao measureDao;

	//新增
	@Override
	public Bo insert(Station station) {
		log.info("后端业务层:工位管理:新增工位:"+station);
		//参数校验
		if (station.getEquipmentId() == null) {
			return new Bo(false, 400, "所选设备不能为空",0,null);
		}else if (station.getStationname() ==null || station.getStationname().equals("")) {
			return new Bo(false, 400, "工位编号不能为空",0,null);
		}else if (station.getNuma()==null) {
			return new Bo(false, 400, "通道a为空",0,null);
		}else if (station.getNumb()==null) {
			return new Bo(false, 400, "通道b为空",0,null);
		}else if (station.getNuma()>12 || station.getNuma()<0) {
			return new Bo(false, 400, "所选通道a不正确",0,null);
		}else if (station.getNumb()>12 || station.getNumb()<0) {
			return new Bo(false, 400, "所选通道b不正确",0,null);
		}else {
			JsonResult insertServer = insertServer(station);
			if (insertServer.getCode()!=200) {
				log.error("后端:工位:新增失败:"+insertServer.getData().toString());
				return new Bo(false, 500, "存储数据失败", 0,null);
			}else {
				log.info("后端:工位:新增成功:");
				return new Bo(false, 200, "存储数据成功", 1,0);
			}
		}
	}

	//修改
	@Override
	public Bo update(Station station) {
		log.info("后端业务层:工位管理:修改工位:"+station);
		if (station.getEquipmentId() == null) {
			return new Bo(false, 400, "所选设备不能为空",0,null);
		}else if (station.getStationname() ==null || station.getStationname().equals("")) {
			return new Bo(false, 400, "工位编号不能为空",0,null);
		}else if (station.getNuma()==null) {
			return new Bo(false, 400, "通道a为空",0,null);
		}else if (station.getNumb()==null) {
			return new Bo(false, 400, "通道b为空",0,null);
		}else if (station.getNuma()>12 || station.getNuma()<0) {
			return new Bo(false, 400, "所选通道a不正确",0,null);
		}else if (station.getNumb()>12 || station.getNumb()<0) {
			return new Bo(false, 400, "所选通道b不正确",0,null);
		}else {
			JsonResult insertServer = updateServer(station);
			if (insertServer.getCode()!=200) {
				log.error("后端:工位:修改失败:"+insertServer.getData().toString());
				return new Bo(false, 500, "修改数据失败", 0,null);
			}else {
				log.info("后端:工位:修改成功:");
				return new Bo(false, 200, "修改数据成功", 1,0);
			}
		}
	}

	//删除
	@Override
	public Bo delete(Integer id) {
		log.info("后端业务层:工位管理:根据id删除工位:"+id);
		if (id<=0) {
			return new Bo(false, 400, "删除工位id编号不能小于0", 0,null);
		}else {
			int size=0;
			try {
				size = measureDao.selectByStationId(id);
				log.info("后端业务层:工位管理:根据id删除工位:查询该工位的记录:"+size);
			} catch (Exception e) {
				log.error("后端业务层:工位管理:根据id删除工位:查询该工位的记录失败:"+e);
				return new Bo(false, 500, "由于查询该工位下工位信息失败,故删除失败", 1,e);
			}

			if (size>0) {
				return new Bo(false, 400, "由于此工位下具有工位信息,不能删除该设备", 0,null);
			}else {
				JsonResult insertServer = deleteServer(id);
				if (insertServer.getCode()!=200) {
					log.error("后端业务层:工位管理:删除失败:"+insertServer.getData().toString());
					return new Bo(false, 500, "删除数据失败", 0,null);
				}else {
					log.info("后端:工位:删除成功:");
					return new Bo(false, 200, "删除数据成功", 1,0);
				}
			}
		}
	}
	//分页查全部
	@Override
	public Bo select(Integer pages, Integer line) {
		log.info("后端业务层:工位管理:分页查询:pages="+pages+",line="+line);
		//数据校验
		if (pages==null) {
			pages=1;
		}
		if (line==null) {
			line=20;
		}	
		int a = (pages-1)*line;
		try {
			List<StationShow> select = stationDao.select(a, line);
			log.info("后端业务层:工位管理:分页查询:pages="+pages+",line="+line+"结果:"+select);
			return new Bo(true, 200, "查询设备成功",select.size(),select);
		} catch (Exception e) {
			log.error("后端业务层:工位管理:分页查询失败:"+e);
			return new Bo(false, 500, "查询设备失败", 1,e);
		}
	}
	//查询总数
	@Override
	public Bo selectSize() {
		log.info("后端业务层:工位管理:查询工位总数");
		try {
			int size = stationDao.selectSize();
			log.info("后端业务层:工位管理:查询工位总数结果:"+size);
			return new Bo(true, 200, "查询设备数量成功",1,size);
		} catch (Exception e) {
			log.error("后端业务层:工位管理:查询工位总数失败"+e);
			return new Bo(false, 500, "查询设备数量失败", 1,e);
		}
	}

	@Override
	public Bo selectById(Integer id) {
		log.info("后端业务层:工位管理:根据id查询工位:"+id);
		//数据校验
		//数据校验
		if (id<=0) {
			return new Bo(false, 400, "查询设备id编号不能小于0", 0,null);
		}else {
			try {
				StationShow station = stationDao.selectById(id);
				log.info("后端业务层:工位管理:根据id查询工位:"+id+"结果:"+station);
				return new Bo(true, 200, "id查询设备成功",1,station);
			} catch (Exception e) {
				log.error("后端业务层:工位管理:根据id查询工位失败:"+e);
				return new Bo(false, 500, "id查询设备失败", 1,e);
			}
		}
	}

	@Override
	public Bo selectByequipmentId(Integer equipmentId) {
		log.info("后端业务层:工位管理:根据设备id查询工位:"+equipmentId);
		try {
			List<StationShow> select = stationDao.selectByequipmentId(equipmentId);
			log.info("后端业务层:工位管理:根据设备id查询工位结果:"+select);
			return new Bo(true, 200, "根据设备查询工位成功", select.size(), select);
		} catch (Exception e) {
			log.error("后端业务层:工位管理:根据设备id查询工位失败:"+e);
			return new Bo(false, 500, "根据设备id查询工位失败", 1,e);
		}
		
	}

	@Override
	public Bo selectByequipmentIds(Integer[] equipmentIds) {
		log.info("后端业务层:工位管理:根据设备id数组查询工位:"+equipmentIds);
		try {
			List<Station> select = stationDao.selectByequipmentIds(equipmentIds);
			log.info("后端业务层:工位管理:根据设备id数组查询工位:"+equipmentIds+"结果:"+select);
			return new Bo(true, 200, "根据设备id数组查询工位成功", select.size(), select);
		
		} catch (Exception e) {
			log.error("后端业务层:工位管理:根据设备id数组查询工位失败:"+e);
			return new Bo(false, 500, "根据设备id数组查询工位失败", 1,e);
		}
		
	}

	@Override
	public Bo webinsert(Station station) {
		log.info("后端:工位:服务器新增:"+station);
		try {
			int a = stationDao.insert(station);
			log.info("后端:工位:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:工位:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webupdate(Station station) {
		log.info("后端:工位:服务器新增:"+station);
		try {
			int a = stationDao.update(station);
			log.info("后端:工位:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:工位:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webdelete(Integer id) {
		log.info("后端:工位:服务器新增:"+id);
		try {
			int a = stationDao.delete(id);
			log.info("后端:工位:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:工位:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webselect() {
		log.info("后端:工位:服务器新增:");
		try {
			ArrayList<Station> list =stationDao.webselect();
			log.info("后端:工位:服务器新增成功:"+list);
			return new Bo(true, 200, "服务器新增成功",1,list);
		} catch (Exception e) {
			log.error("后端:工位:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}

	
	private JsonResult insertServer(Station station) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/station/insert?id="+station.getId()
		+"&equipmentId="+station.getEquipmentId()
		+"&stationname="+station.getStationname()
		+"&numa="+station.getNuma()
		+"&numb="+station.getNumb()
		+"&remarks="+station.getRemarks();
		try {
			log.info("后端:工位:同步新增:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:工位:同步新增:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert =stationDao.insert(station);
				log.info("后端:工位:同步新增:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:工位:同步新增:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
		
		
	}

	private JsonResult updateServer(Station station) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/station/update?id="+station.getId()
		+"&equipmentId="+station.getEquipmentId()
		+"&stationname="+station.getStationname()
		+"&numa="+station.getNuma()
		+"&numb="+station.getNumb()
		+"&remarks="+station.getRemarks();
		try {
			log.info("后端:工位:同步修改:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:工位:同步修改:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				log.info("后端:工位:同步修改:"+httpString);
				int insert = stationDao.update(station);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:工位:同步修改:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
	}

	private JsonResult deleteServer(Integer id) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/station/delete?id="+id;
		try {
			log.info("后端:工位:同步删除:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:工位:同步删除:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = stationDao.delete(id);
				log.info("后端:工位:同步删除:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:工位:同步删除:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
	}


}
